package telasDeCadastros;

import Geral.Menu;
import entidade.Funcionario;
import java.sql.Date;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import persistencia.BD;

/**
 *
 * @author Thomas Magnno Martins
 *         Alexandre Junior Martins
 *         Wanderson Ferreira Barboza
 */
public class CadastroFuncionario2 extends javax.swing.JFrame {

    Funcionario funcionario;

    public CadastroFuncionario2() {
        initComponents();
    }

    public void inicializar(Funcionario f) {
        this.setVisible(true);
        funcionario = f;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        banner = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jLabel45 = new javax.swing.JLabel();
        jLabel46 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        txtTelefone = new javax.swing.JTextField();
        jLabel47 = new javax.swing.JLabel();
        txtTipoLogradouro = new javax.swing.JTextField();
        jLabel48 = new javax.swing.JLabel();
        txtLogradouro = new javax.swing.JTextField();
        jLabel49 = new javax.swing.JLabel();
        txtCep = new javax.swing.JTextField();
        jLabel50 = new javax.swing.JLabel();
        txtBairro = new javax.swing.JTextField();
        jLabel51 = new javax.swing.JLabel();
        txtcidade = new javax.swing.JTextField();
        jLabel52 = new javax.swing.JLabel();
        txtUf = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNumero = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        txtRg = new javax.swing.JTextField();
        jLabel23 = new javax.swing.JLabel();
        txtPisPasep = new javax.swing.JTextField();
        txtCpf = new javax.swing.JFormattedTextField();
        lblMensagem = new javax.swing.JLabel();
        btnSair = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnVoltar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 718, 740));
        setResizable(false);

        banner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/telasDeCadastros/banner.jpg"))); // NOI18N
        banner.setAutoscrolls(true);
        banner.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true));
        banner.setMaximumSize(new java.awt.Dimension(720, 187));
        banner.setMinimumSize(new java.awt.Dimension(720, 187));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 16));
        jLabel1.setForeground(new java.awt.Color(0, 102, 204));
        jLabel1.setText("Cadastro de Funcionário");

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true), "Endereço", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 11))); // NOI18N
        jPanel8.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel45.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel45.setText("Telefone : ");
        jPanel8.add(jLabel45, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 164, -1, -1));

        jLabel46.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel46.setText("E-mail : ");
        jPanel8.add(jLabel46, new org.netbeans.lib.awtextra.AbsoluteConstraints(72, 138, -1, -1));
        jPanel8.add(txtEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(118, 135, 537, -1));
        jPanel8.add(txtTelefone, new org.netbeans.lib.awtextra.AbsoluteConstraints(118, 161, 364, -1));

        jLabel47.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel47.setText("Tipo Logradouro :");
        jPanel8.add(jLabel47, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 57, -1, -1));
        jPanel8.add(txtTipoLogradouro, new org.netbeans.lib.awtextra.AbsoluteConstraints(118, 57, 364, -1));

        jLabel48.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel48.setText("Logradouro : ");
        jPanel8.add(jLabel48, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 34, -1, -1));
        jPanel8.add(txtLogradouro, new org.netbeans.lib.awtextra.AbsoluteConstraints(118, 31, 537, -1));

        jLabel49.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel49.setText("CEP : ");
        jPanel8.add(jLabel49, new org.netbeans.lib.awtextra.AbsoluteConstraints(492, 60, -1, -1));

        txtCep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCepActionPerformed(evt);
            }
        });
        jPanel8.add(txtCep, new org.netbeans.lib.awtextra.AbsoluteConstraints(526, 57, 129, -1));

        jLabel50.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel50.setText("Bairro : ");
        jPanel8.add(jLabel50, new org.netbeans.lib.awtextra.AbsoluteConstraints(72, 86, -1, -1));
        jPanel8.add(txtBairro, new org.netbeans.lib.awtextra.AbsoluteConstraints(118, 83, 364, -1));

        jLabel51.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel51.setText("Cidade : ");
        jPanel8.add(jLabel51, new org.netbeans.lib.awtextra.AbsoluteConstraints(67, 112, -1, -1));
        jPanel8.add(txtcidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(118, 109, 364, -1));

        jLabel52.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel52.setText("UF : ");
        jPanel8.add(jLabel52, new org.netbeans.lib.awtextra.AbsoluteConstraints(492, 112, -1, -1));
        jPanel8.add(txtUf, new org.netbeans.lib.awtextra.AbsoluteConstraints(526, 109, 129, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Nº : ");
        jPanel8.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(492, 86, -1, -1));
        jPanel8.add(txtNumero, new org.netbeans.lib.awtextra.AbsoluteConstraints(526, 83, 129, -1));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true), "Documentação", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 11))); // NOI18N
        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel21.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel21.setText("RG :");
        jPanel5.add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(96, 65, -1, -1));

        jLabel22.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel22.setText("CPF : ");
        jPanel5.add(jLabel22, new org.netbeans.lib.awtextra.AbsoluteConstraints(87, 34, -1, -1));
        jPanel5.add(txtRg, new org.netbeans.lib.awtextra.AbsoluteConstraints(121, 62, 331, -1));

        jLabel23.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel23.setText("PIS/PASEP : ");
        jPanel5.add(jLabel23, new org.netbeans.lib.awtextra.AbsoluteConstraints(53, 91, -1, -1));
        jPanel5.add(txtPisPasep, new org.netbeans.lib.awtextra.AbsoluteConstraints(121, 88, 331, -1));
        jPanel5.add(txtCpf, new org.netbeans.lib.awtextra.AbsoluteConstraints(121, 31, 331, -1));

        lblMensagem.setForeground(new java.awt.Color(255, 0, 0));

        btnSair.setText("Sair");
        btnSair.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true));
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnVoltar.setText("Voltar");
        btnVoltar.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 255), 2, true));
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(235, 235, 235)
                .addComponent(jLabel1))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMensagem, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE))
            .addComponent(banner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 689, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(484, Short.MAX_VALUE)
                .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnSair, btnSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(banner, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(11, 11, 11)
                .addComponent(lblMensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(90, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnSair, btnSalvar});

        setBounds(150, 0, 718, 740);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
}//GEN-LAST:event_btnSairActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed


        BD banco = new BD();
        int idFuncionario = 0;


        if (txtCpf.getText().trim().equals("")) {
            lblMensagem.setText("O campo CPF é obrigatório!");

        } else if (txtRg.getText().trim().equals("")) {
            lblMensagem.setText("O campo RG é obrigatório!");

        } else {
            //Bloco Documentação
            funcionario.cpf = txtCpf.getText().trim().toUpperCase();
            funcionario.rg = txtRg.getText().trim().toUpperCase();
            funcionario.pis = txtPisPasep.getText().trim().toUpperCase();

            //Bloco Endereço
            funcionario.tipoLogradouro = txtTipoLogradouro.getText().trim().toUpperCase();
            funcionario.cep = txtCep.getText().trim().toUpperCase();
            funcionario.logradouro = txtLogradouro.getText().trim().toUpperCase();
            funcionario.numero = txtNumero.getText().trim().toUpperCase();
            funcionario.bairro = txtBairro.getText().trim().toUpperCase();
            funcionario.telefone = txtTelefone.getText().trim().toUpperCase();
            funcionario.cidade = txtcidade.getText().trim().toUpperCase();
            funcionario.uf = txtUf.getText().trim().toUpperCase();
            funcionario.email = txtEmail.getText();

            try {

                String consu = "SELECT SQ_FUNCIONARIO.NEXTVAL AS IDFUNCIONARIO FROM DUAL";
                ResultSet res;
                res = banco.consultar(consu);
                if (res.next()) {
                    idFuncionario = res.getInt("IDFUNCIONARIO");
                }

                String sql = "INSERT INTO FUNCIONARIO (IDFUNCIONARIO, COD_FUN, NOME_FUN, DATANASC_FUN, "
                        + " RG_FUN, CPF_FUN, NACION_FUN, NATURAL_FUN, SEXO_FUN, NOMEPAI_FUN, NOMEMAE_FUN, "
                        + " CARGO_FUN, FUNCAO_FUN, TIPOLOGRA_FUN, LOGRA_FUN, CEP_FUN, BAIRRO_FUN, CIDADE_FUN, "
                        + " UF_FUN, TEL_FUN, EMAIL_FUN, ESTCIVIL_FUN, NUM_FUN, PIS_FUN, DATAADMIN_FUN, DATADEMIS_FUN) "
                        + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,"
                        + " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

                Object[] parametros = {idFuncionario, funcionario.codigo, funcionario.nome, funcionario.dtnasc,
                    funcionario.rg, funcionario.cpf, funcionario.nacionalidade, funcionario.naturalidade,
                    funcionario.sexo, funcionario.nomepai, funcionario.nomemae, funcionario.cargo,
                    funcionario.funcao, funcionario.tipoLogradouro, funcionario.logradouro,
                    funcionario.cep, funcionario.bairro, funcionario.cidade, funcionario.uf,
                    funcionario.telefone, funcionario.email, funcionario.estadocivil,
                    funcionario.numero, funcionario.pis, funcionario.dataadmin, funcionario.datademis};

                Object[] tipos = {Integer.class, String.class, String.class, Date.class, String.class, String.class,
                    String.class, String.class, String.class, String.class, String.class,
                    String.class, String.class, String.class, String.class, String.class,
                    String.class, String.class, String.class, String.class, String.class,
                    String.class, String.class, String.class, Date.class, Date.class};

                String sqldois = "INSERT INTO USUARIO (LOGIN, SENHA, IDFUNCIONARIO) "
                        + " VALUES ('" + funcionario.codigo + "', '" + funcionario.codigo + "', " + idFuncionario + " )  ";

                Object[] parametrosdois = {};
                Object[] tiposdois = {};

                banco.executar(sql, parametros, tipos);
                banco.executar(sqldois, parametrosdois, tiposdois);


                JOptionPane.showMessageDialog(this, "Gravado com Sucesso");

                Object[] options = {"Sim", "Não"};
                int i = JOptionPane.showOptionDialog(null,
                        "Deseja Cadastrar mais Funcionarios?", "",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        options, options[0]);

                if (i == 0) {
                    CadastroFuncionario cadastroFuncionario = new CadastroFuncionario();
                    cadastroFuncionario.setVisible(true);
                    this.dispose();

                } else if (i == 1) {

                    Menu menu = new Menu();
                    menu.setVisible(true);
                    this.dispose();
                }

               

            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void txtCepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCepActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCepActionPerformed

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed

        this.dispose();
        CadastroFuncionario funcionario = new CadastroFuncionario();
        funcionario.setVisible(true);

    }//GEN-LAST:event_btnVoltarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadastroFuncionario2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel banner;
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel45;
    private javax.swing.JLabel jLabel46;
    private javax.swing.JLabel jLabel47;
    private javax.swing.JLabel jLabel48;
    private javax.swing.JLabel jLabel49;
    private javax.swing.JLabel jLabel50;
    private javax.swing.JLabel jLabel51;
    private javax.swing.JLabel jLabel52;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JLabel lblMensagem;
    private javax.swing.JTextField txtBairro;
    private javax.swing.JTextField txtCep;
    private javax.swing.JFormattedTextField txtCpf;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtLogradouro;
    private javax.swing.JTextField txtNumero;
    private javax.swing.JTextField txtPisPasep;
    private javax.swing.JTextField txtRg;
    private javax.swing.JTextField txtTelefone;
    private javax.swing.JTextField txtTipoLogradouro;
    private javax.swing.JTextField txtUf;
    private javax.swing.JTextField txtcidade;
    // End of variables declaration//GEN-END:variables
}
